def set_zeros(matrix):

    # 子函数
    def clean_row(row):
        for i in range(len(matrix[0])):
            matrix[row][i] = 0

    def clean_col(col):
        for i in range(len(matrix[0])):
            matrix[i][col] = 0

    # 第一行、第一列是否有0，保存状态
    row_has_zero, col_has_zero = False, False
    for i in range(len(matrix[0])):
        if matrix[0][i] == 0:
            row_has_zero = True
            break
    for i in range(len(matrix)):
        if matrix[i][0] == 0:
            col_has_zero = True
            break
    # 用第一列、第一行保存待清零的行、列
    for i in range(1, len(matrix)):
        for j in range(1, len(matrix[0])):
            if matrix[i][j] == 0:
                matrix[i][0] = 0
                matrix[0][i] = 0
    # 清零
    for i in range(0, len(matrix)):
        if matrix[i][0] == 0:
            clean_row(i)
    for i in range(0, len(matrix[0])):
        if matrix[0][i] == 0:
            clean_col(i)
    # 处理第一行、第一列
    if row_has_zero:
        clean_row(0)
    if col_has_zero:
        clean_col(0)


from e1_7 import matrix1
from print_matrix import print_matrix
set_zeros(matrix1)
print_matrix(matrix1)
